/**
 * Copyright (c) 2018 软唐 All rights reserved.
 *
 * https://www.softtown.cn
 *
 * 版权所有，侵权必究！
 */

package com.softtown.modules.dapai.controller;

import com.softtown.common.annotation.LogOperation;
import com.softtown.common.constant.Constant;
import com.softtown.common.exception.ErrorCode;
import com.softtown.common.page.PageData;
import com.softtown.common.regularUtils.RegularUtils;
import com.softtown.common.utils.Result;
import com.softtown.common.validator.AssertUtils;
import com.softtown.common.validator.ValidatorUtils;
import com.softtown.common.validator.group.AddGroup;
import com.softtown.common.validator.group.DefaultGroup;
import com.softtown.common.validator.group.UpdateGroup;
import com.softtown.modules.dapai.dto.CompCityDTO;
import com.softtown.modules.dapai.dto.CompCustomerDTO;
import com.softtown.modules.dapai.entity.CompCityEntity;
import com.softtown.modules.dapai.service.CompCityService;
import com.softtown.modules.dapai.vo.CompCityVO;
import com.softtown.modules.demo.dto.NewsDTO;
import com.softtown.modules.demo.service.NewsService;
import com.softtown.modules.organization.dto.CompOrgDeptDTO;
import com.softtown.modules.organization.vo.CompOrgDeptVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 地区表
 *
 * @author Ding Ya Yong
 */
@RestController
@RequestMapping("city")
@Api(tags="地区管理")
public class CompCityController {

    @Resource
    private CompCityService compCityService;

    @PostMapping("getTreeCity")
    @ApiOperation("获取城市信息【树形图】")
    public Result<List<CompCityVO>> getTreeCity(@RequestBody CompCityDTO compCityDTO) {
        List<CompCityVO> deptListByCompId = compCityService.getTreeCity(compCityDTO.getCompId());
        return new Result<List<CompCityVO>>().ok(deptListByCompId);
    }

    @PostMapping("getProvinceCities")
    @ApiOperation("获取所有省份")
    public Result<List<CompCityVO>> getProvinceCities(@RequestBody CompCityDTO compCityDTO) {
        List<CompCityVO> deptListByCompId = compCityService.getProvinceCities(compCityDTO.getCompId());
        return new Result<List<CompCityVO>>().ok(deptListByCompId);
    }

    @PostMapping("getCityListById")
    @ApiOperation("获取上级为id的城市信息")
    public Result<List<CompCityVO>> getCityListById(@RequestBody CompCityDTO compCityDTO) {
        List<CompCityVO> deptListByCompId = compCityService.getCityListById(compCityDTO.getCompId(),compCityDTO.getId());
        return new Result<List<CompCityVO>>().ok(deptListByCompId);
    }

    @PostMapping("deleteCity")
    @ApiOperation("删除省市区")
    public Result deleteCity(@RequestBody CompCityDTO compCityDTO) {
        Result result = new Result<>();
        List<CompCityVO> deptListByCompId = compCityService.getCityListById(compCityDTO.getCompId(),compCityDTO.getId());
        if(deptListByCompId.size() >0 ){
            return result.error(ErrorCode.CITY_NEXT_EXISTS);
        }
        compCityService.deleteCity(compCityDTO.getCompId(),compCityDTO.getId());
        return result;
    }



    @PostMapping("addCity")
    @ApiOperation("添加省市区")
    public Result addCity(@RequestBody CompCityDTO compCityDTO) {
        Result result = new Result<>();
        //校验数据是否为空
        if (!RegularUtils.isNum(String.valueOf(compCityDTO.getSort()))) {
            return result.error(ErrorCode.INCORRECT_SORT_FORMAT);
        }
        List<CompCityVO> cityList = compCityService.getCityList(compCityDTO.getCompId());
        for (CompCityVO compCityVO:cityList) {
            String deptNames = compCityVO.getName();
            if (compCityDTO.getName().equals(deptNames) && compCityVO.getPid().equals(compCityDTO.getPid())) {
                return result.error(ErrorCode.CITY_NAME_EXISTS);
            }
        }
        compCityService.addCity(compCityDTO);
        return result;
    }


    @PostMapping("updateCity")
    @ApiOperation("修改省市区")
    public Result updateCity(@RequestBody CompCityDTO compCityDTO) {
        Result result = new Result<>();
        //校验数据是否为空
        if (RegularUtils.isDeptName(compCityDTO.getName())) {
            return result.error(ErrorCode.INCORRECT_DEPARTMENT_FORMAT);
        }
        if (!RegularUtils.isNum(String.valueOf(compCityDTO.getSort()))) {
            return result.error(ErrorCode.INCORRECT_SORT_FORMAT);
        }
        List<CompCityVO> cityList = compCityService.getCityList(compCityDTO.getCompId());
        for (CompCityVO compCityVO:cityList) {
            String deptNames = compCityVO.getName();
            if (compCityDTO.getName().equals(deptNames) && compCityVO.getPid().equals(compCityDTO.getPid())) {
                if (!compCityVO.getId().equals(compCityDTO.getId())){
                    return result.error(ErrorCode.CITY_NAME_EXISTS);
                }

            }
        }
        compCityService.updateCity(compCityDTO);
        return result;
    }


}